package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachPlanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.content.common.utils.UAASecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachPlanApi {

    @Autowired
    private TeachplanService teachplanService;

    /**
     * 根据课程id查询课程计划树型结构
     *
     * @param courseBaseId
     * @return
     */
    @GetMapping("teachplan/{courseBaseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseBaseId(@PathVariable Long courseBaseId) {
        //1.获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        //查询课程计划数据,并返回
        TeachplanDTO teachplanDTO = this.teachplanService.queryTreeNodesByCourseBaseId(courseBaseId, companyId);
        return teachplanDTO;

    }

    /**
     * 添加或修改课程计划
     *
     * @param vo
     * @return
     */
    @Override
    @PostMapping("teachplan")
    public TeachplanDTO createOrmodifyTeachplan(@RequestBody TeachplanVO vo) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        TeachplanDTO teachplanDTO = TeachplanConvert.INSTANCE.vo2dto(vo);
        TeachplanDTO dto = this.teachplanService.createOrmodifyTeachplan(teachplanDTO, companyId);
        return dto;
    }

    /**
     * 根据课程计划id删除课程计划
     *
     * @param teachplanId 课程计划id
     */
    @Override
    @DeleteMapping("teachplan/{teachplanId}")
    public void removeTeachplan(@PathVariable Long teachplanId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        this.teachplanService.removeTeachplan(teachplanId, companyId);
    }

    /**
     * 课程计划绑定媒资信息
     *
     * @param vo 绑定信息
     * @return
     */
    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        //设置基础信息
        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();
        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());

        TeachplanMediaDTO result = this.teachplanService.associateMedia(teachplanMediaDTO, companyId);
        return result;
    }


}
